package com.sanji.easyui.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by joe on 16-12-29.
 */
@Controller
@RequestMapping("/user")
public class UserController {

  private static List<User> users;
  static {
    users = new ArrayList<>();
    User u1 = new User("li","si","123","12qq.com");
    User u2 = new User("wang","si","123","12qq.com");
    User u3 = new User("xiao","si","123","12qq.com");
    User u4 = new User("ha","si","123","12qq.com");
    User u5 = new User("hei","si","123","12qq.com");
    users.add(u1);
    users.add(u2);
    users.add(u3);
    users.add(u4);
    users.add(u5);
  }

  /**
   * 查询全部
   * @return
   */
  @RequestMapping(value = "/findAll",method = RequestMethod.POST)
  @ResponseBody
  public List<User> findAll(){

    return  users;
  }

  /**
   * 添加
   * @param user
   * @return
   */
  @RequestMapping(value = "/save",method = RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> save(User user){
    users.add(user);
    Map<String,Object> map = new HashMap<>();
    map.put("success",true);
    map.put("msg","添加成功");
    return map;
  }

  /**
   * 修改
   * @param user
   * @return
   */
  @RequestMapping(value = "/edit",method = RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> edit(User user){
    System.out.println(user);
    Map<String,Object> map = new HashMap<>();
    map.put("success",true);
    map.put("msg","修改成功");
    return map;
  }


  /**
   * s删除
   * @param
   * @return
   */
  @RequestMapping(value = "/edit",method = RequestMethod.POST)
  @ResponseBody
  public Map<String,Object> remove(String account){
    Map<String,Object> map = new HashMap<>();
    map.put("success",true);
    map.put("msg","修改成功");
    return map;
  }

}


class User{
  private String firstname;
  private String lastname;
  private String phone;
  private String email;

  public User(){}
  public User(String firstname,String lastname,String phone,String email){
    this.firstname = firstname;
    this.lastname = lastname;
    this.phone = phone;
    this.email = email;
  }

  public String getFirstname() {
    return firstname;
  }

  public void setFirstname(String firstname) {
    this.firstname = firstname;
  }

  public String getLastname() {
    return lastname;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  @Override
  public String toString() {
    return "User{" +
        "firstname='" + firstname + '\'' +
        ", lastname='" + lastname + '\'' +
        ", phone='" + phone + '\'' +
        ", email='" + email + '\'' +
        '}';
  }
}